package com.sctvcloud.qingchuan.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.luck.picture.lib.PictureExternalPreviewActivity;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.base.BaseFragment;
import com.sctvcloud.qingchuan.beans.PoliticsItem;
import com.sctvcloud.qingchuan.ui.adapter.PoliticsItemImgAdapter;
import com.sctvcloud.qingchuan.ui.utils.SkipUtil;
import com.sctvcloud.qingchuan.ui.utils.UIUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 问政列表单元显示
 */
public class PoliticsItemHolder extends BaseAbsHolder<PoliticsItem> implements View.OnClickListener {

    @BindView(R.id.item_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_content)
    protected CustomFontTextView content;
    @BindView(R.id.item_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_status)
    protected CustomFontTextView status;
    @BindView(R.id.item_read_count)
    protected CustomFontTextView readCount;
    @BindView(R.id.divider)
    protected View divider;
    @BindView(R.id.item_image_layout)
    protected LinearLayout imageLayout;
    @BindView(R.id.item_only_img)
    protected ImageView itemOnlyImg;
    @BindView(R.id.item_img_list)
    protected RecyclerView itemImgListView;

    @BindView(R.id.item_video_layout)
    protected FrameLayout videoLayout;
    @BindView(R.id.item_video_img)
    protected ImageView videoImage;

    private BaseFragment fragment;
    private PoliticsItemImgAdapter imgAdapter;
    private PoliticsItem temp;
    private GridLayoutManager mgr;


    public PoliticsItemHolder(BaseFragment fragment, Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.fragment = fragment;
        mgr = new GridLayoutManager(context, 3);
    }


    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(PoliticsItem t) {
        temp = t;
        String clickCount = UIUtils.getPoliticsListNumber(t.getHitCount());
        readCount.setText(clickCount + "阅读");
        title.setText(t.getTitle());
        content.setText(t.getContent());
        time.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
        if (t.getOperationStatus() == 1) {
            status.setText("待处理");
            status.setTextColor(content.getResources().getColor(R.color.colorHomeText));
        } else if (t.getOperationStatus() == 3) {
            status.setText("处理中");
            status.setTextColor(content.getResources().getColor(R.color.colorHomeText));
        } else if (t.getOperationStatus() == 4) {
            if (t.getScore() == 0) {
                status.setText("已回复");
                status.setTextColor(content.getResources().getColor(R.color.colorHomeText));
            } else {
                status.setText("已解决");
                status.setTextColor(content.getResources().getColor(R.color.colorGray98));
            }
        } else if (t.getOperationStatus() == 7) {
            status.setText("已驳回");
            status.setTextColor(content.getResources().getColor(R.color.colorRedMain_));

        }else{
            // 8 已转发（也是处理中）
            status.setText("处理中");
            status.setTextColor(content.getResources().getColor(R.color.colorHomeText));
        }

        if (ListUtils.isListValued(t.getInteractioninfoImageList())) {
            imageLayout.setVisibility(View.VISIBLE);
            if (t.getInteractioninfoImageList().size() == 1) {
                final String path = t.getInteractioninfoImageList().get(0).getImagePath();
                itemOnlyImg.setVisibility(View.VISIBLE);
                itemImgListView.setVisibility(View.GONE);
                initImgView(path);
                itemOnlyImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<LocalMedia> images = new ArrayList<LocalMedia>();

                        LocalMedia localMedia = new LocalMedia();
                        localMedia.setPath(path);
                        images.add(localMedia);

                        Intent intent = new Intent(context, PictureExternalPreviewActivity.class);
                        intent.putExtra("type", 1);
                        intent.putExtra(PictureConfig.EXTRA_PREVIEW_SELECT_LIST, (Serializable) images);
                        intent.putExtra(PictureConfig.EXTRA_POSITION, 0);
                        context.startActivity(intent);
                    }
                });
            } else {
                itemOnlyImg.setVisibility(View.GONE);
                itemImgListView.setVisibility(View.VISIBLE);
                itemImgListView.setLayoutManager(mgr);
                imgAdapter = new PoliticsItemImgAdapter(context, t.getInteractioninfoImageList());
                itemImgListView.setAdapter(imgAdapter);

            }
        } else {
            imageLayout.setVisibility(View.GONE);
        }

        if (ListUtils.isListValued(t.getInteractioninfoVideoList())) {
            final String path = t.getInteractioninfoVideoList().get(0).getLogo();
            final String videoUrl = t.getInteractioninfoVideoList().get(0).getUrl();
            videoLayout.setVisibility(View.VISIBLE);
            initVideoImgView(path);
            videoLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SkipUtil.skipToVideoPlayWithCover(fragment, videoUrl, 0, 0, path);
                }
            });

        } else {
            videoLayout.setVisibility(View.GONE);
        }
    }

    private void initImgView(final String path) {
        Glide.with(context)
                .load(path)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .centerCrop()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) itemOnlyImg.getLayoutParams();
                        if (height > width) {
                            if (height > UIUtils.dip2px(context, 240)) {
                                params.width = UIUtils.dip2px(context, 178);
                                params.height = UIUtils.dip2px(context, 240);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        } else if (height < width) {
                            if (width > UIUtils.dip2px(context, 240)) {
                                params.height = UIUtils.dip2px(context, 178);
                                params.width = UIUtils.dip2px(context, 240);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        } else {
                            JLog.e("-----onResourceReady---width = " + width + "---------height = " + height);
                            if (height > UIUtils.dip2px(context, 178)) {
                                params.height = UIUtils.dip2px(context, 178);
                                params.width = UIUtils.dip2px(context, 178);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        }
                        itemOnlyImg.setLayoutParams(params);
//                        itemOnlyImg.setImageBitmap(bitmap);
                        Glide.with(context)
                                .load(path)
                                .error(com.luck.picture.lib.R.drawable.icon_def_1_1)
                                .into(itemOnlyImg);
                    }
                });
    }

    private void initVideoImgView(final String path) {
        Glide.with(context)
                .load(path)
                .asBitmap()//强制Glide返回一个Bitmap对象
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) videoImage.getLayoutParams();
                        if (height > width) {
                            if (height > UIUtils.dip2px(context, 178)) {
                                params.width = UIUtils.dip2px(context, 100);
                                params.height = UIUtils.dip2px(context, 178);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        } else if (height < width) {
                            if (width > UIUtils.dip2px(context, 178)) {
                                params.height = UIUtils.dip2px(context, 100);
                                params.width = UIUtils.dip2px(context, 178);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        } else {
                            if (height > UIUtils.dip2px(context, 100)) {
                                params.height = UIUtils.dip2px(context, 100);
                                params.width = UIUtils.dip2px(context, 100);
                            } else {
                                params.height =  height ;
                                params.width =  width ;
                            }
                        }
                        videoImage.setLayoutParams(params);
                        Glide.with(context)
                                .load(path)
                                .centerCrop().error(com.luck.picture.lib.R.drawable.icon_def_1_1)
                                .into(videoImage);
                    }
                });

    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
//        if (mode == ITEMMODE_LAST) {
//            bottomLine.setVisibility(View.GONE);
//        } else {
////            bottomLine.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }

        //TODO 进入问政详情
        SkipUtil.skipToPoliticsInfo(context, temp.getId());
    }

}
